<?php

declare (strict_types = 1);

namespace auth;

use InvalidArgumentException;

trait CreatesUserProvider
{
    /**
     * @param  string|null  $provider
     * @return \auth\contract\UserProvider|null
     *
     * @throws \InvalidArgumentException
     */
    public function createUserProvider(string $provider = null): ?contract\UserProvider
    {
        if (is_null($config = config('auth.providers.'.$provider))) {
            return null;
        }

        $driver = $config['driver'] ?? null;

        switch ($driver) {
            case 'orm':
                return $this->createOrmProvider($config);
            default:
                throw new InvalidArgumentException(
                    "Authentication user provider [{$driver}] is not defined."
                );
        }
    }

    /**
     * @param $config
     * @return \auth\OrmUserProvider
     */
    protected function createOrmProvider($config): OrmUserProvider
    {
        return new OrmUserProvider($this->app['hash'], $config['model']);
    }
}
